package reflection;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/**
 * @author 吴阳基
 * @datetime 2024/1/3 13:43
 */
public class ShiLi3 {
    @Test
    public void test() {

        Constructor<?>[] cons = Person.class.getDeclaredConstructors();
        System.out.println("构造方法");
        for (Constructor<?> con : cons) {
            System.out.println("访问修饰符：");
            int modifiers = con.getModifiers(); // 获取构造方法的修饰符
            if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC) {
                System.out.println("public");
            } else if ((modifiers & Modifier.PRIVATE) == Modifier.PRIVATE) {
                System.out.println("private");
            } else if ((modifiers & Modifier.PROTECTED) == Modifier.PROTECTED) {
                System.out.println("protected");
            }

            Class<?>[] params = con.getParameterTypes();
            if (params.length == 0) {
                System.out.println("无参数");
            } else {
                System.out.print("该构造方法的参数列表为：[");
                for (int i = 0; i < params.length; i++) {
                    if (i != 0) {
                        System.out.print(",");

                    }
                    System.out.print(params[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("--------------------------");

        }
    }
}
